<?php

class Office extends AppModel {

    //var $useDbConfig = 'service';
    public $name = 'Office';

    public $hasMany = array(
        /*
        'Advertise' => array(
            'className' => 'Advertise',
            'foreignKey' => 'office_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Coupon' => array(
            'className' => 'Coupon',
            'foreignKey' => 'office_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'FreeadrsArea' => array(
            'className' => 'FreeadrsArea',
            'foreignKey' => 'office_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'FreeadrsCrowded' => array(
            'className' => 'FreeadrsCrowded',
            'foreignKey' => 'office_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Rental' => array(
            'className' => 'Rental',
            'foreignKey' => 'office_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Room' => array(
            'className' => 'Room',
            'foreignKey' => 'office_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        */
		'ChildOffice' => array('className' => 'Office',
              'foreignKey' => 'parent_office_id',
              'fields' => 'id, name, parent_office_id, type',
        ),
    );
    public $belongsTo = array(
        'ParentOffice' => array(
            'className' => 'Office',
            'foreignKey' => 'parent_office_id'
        )
    );

    /*
     * @description: getAll office
     * @since : 20/12/2013
     * @author: nguyennb6390@setacinq.com.vn
     */

    public function getAll() {
        // MiengTQ MOD check if close_dt is Null and status = 1
        $results = $this->query("SELECT 
                                    id,
                                    parent_office_id,
                                    name,
                                    type,
                                    open_hour,
                                    close_hour,
                                    type,
                                    holiday,
                                    close_dt
                                FROM offices 
                                WHERE 
                                    type <> 10 
                                    AND 
                                    ((NOW() BETWEEN open_dt AND close_dt) OR (NOW() > open_dt AND close_dt IS NULL)) AND
                                    status = 1");
        return $results;
    }

    /*
     * @description: getByDatCal
     * @since : 17/1/2014
     * @author: nguyennb6390@setacinq.com.vn
     */

    public function getByDatCal($date) {
        $results = $this->query("SELECT
                                    id,
                                    parent_office_id,
                                    name,
                                    type,
                                    open_hour,
                                    close_hour,
                                    holiday,
                                    close_dt
                                FROM offices 
                                WHERE 
                                        type <> 10 
                                    AND 
                                        (? BETWEEN DATE_FORMAT(open_dt,'%Y-%m-%d') AND DATE_FORMAT(close_dt,'%Y-%m-%d') OR (? >= DATE_FORMAT(open_dt,'%Y-%m-%d') AND close_dt IS NULL)) 
                                    AND
                                        status = 1", array(date('Y-m-d', strtotime($date)), date('Y-m-d', strtotime($date))));
        return $results;
    }

    /*
     * @description: getByOfficeId
     * @param: int $officeId
     * @since : 20/12/2013
     * @author: nguyennb6390@setacinq.com.vn
     */

    public function getByOfficeId($officeId) {
        $results = $this->query("SELECT id, type, parent_office_id, name, description FROM offices WHERE id = ?", array($officeId));
        return $results;
    }
    /**
     * hasMany associations
     *
     * @var array
     */

    /**
     *
     *@description: get office on id
     *@author: phuongnv
     *@since: Dec 23, 2013
     *@param: id
     *@return: an office with parent and child office
     */

    public function getOfficeById($id) {
        return $this->find('first', array(
            'conditions' => array('Office.id' => $id),
            'fields' => array('Office.id', 'Office.name','Office.parent_office_id', 'Office.type', 'ParentOffice.id', 'ParentOffice.name')
        ));
    }
    /**
     * 
     * Get all groups (group is office with type = 20)
     * @author Nguyen Van Phuong <phuongnv6231@setacinq.com.vn>
     * @since 1.0.0
     * @param: 
     * @return void
     */
    public function getAllGroup() {
        $this->unbindModel(array('belongsTo' => array('ParentOffice')));
        return $this->find('all', array(
            'fields' => array('Office.id', 'Office.name','Office.parent_office_id', 'Office.type'),
            'conditions' => array(
                'Office.status' => 1,
                'Office.type' => 20,
            ),
        ));
    }
    /**
     * 
     * Get office with parent and child by id
     * @author Nguyen Van Phuong <phuongnv6231@setacinq.com.vn>
     * @since 1.0.0
     * @param: 
     * @return void
     * @param unknown_type $id
     */
    public function getOfficeWithParentAndChildeById($id) {
        return $this->find('first', array(
            'fields' => array(
            'Office.id', 'Office.name','Office.parent_office_id', 'Office.type',
            'ParentOffice.id', 'ParentOffice.name','ParentOffice.parent_office_id', 'ParentOffice.type',
        ),
            'conditions' => array(
                'Office.status' => 1,
                'Office.id' => $id
            ),
        ));
    }
    
    /**
    
    * Get open hour and close hour of office
    *
    * @author Nguyen Van Phuong <phuongnv6231@setacinq.com.vn>
    * @since 1.0.0
    * @param: $id
    * @return result of find 
     */
    public function getOpenAndCloseHourById($id) {
        $this->unbindModel(array('hasMany' => array('ChildOffice'), 'belongsTo' => array('ParentOffice')));
        return $this->find('first', array(
            'conditions' => array('Office.id' => $id, 'Office.status' => 1),
            'fields' => array('Office.open_hour', 'Office.close_hour')
        ));
    }

    public function getOfficeByParentIds($parentIds) {
        $this->unbindModel(array('belongsTo' => array('ParentOffice'), 'hasMany' => array('ChildOffice')));
        return $this->find('all', array(
            'conditions' => array('Office.parent_office_id IN ' => $parentIds),
            'fields' => array('Office.id', 'Office.name', 'Office.parent_office_id', 'Office.type')
        ));
    }

    /*
     * @description: getAdvertises
     * @param: int $officeId
     * @param: string $type
     * @since : 20/12/2013
     * @author: nguyennb6390@setacinq.com.vn
     */

    public function getAdvertises($officeId, $type) {
        $results = $this->query("SELECT id, title, content  FROM advertises WHERE office_id = ? AND open_dt <= NOW() AND close_dt >= NOW() AND type = ?", array($officeId, $type));
        return $results;
    }
    /**
     * @author huyhq6159 <huyhq6159@setacinq.com.vn>
     * @param array $data array
     * @param string $$type Type to select
     * @return a findOffice recode or null
     * @example  findOffice(array('office_id' => $officeId) 'first'));
     */
    public function findOffice($data = array(), $type = 'first') {
        if ($data) {
            $conditions = array();
            foreach ($data as $key => $value) {
                $conditions[$key] = $value;
            }
            $query = array('conditions' => $conditions);
            return $this->find($type,$query);
        }
        return;
    }
    public function getGroup($office_id){
        $data= $this->query(
                "SELECT offices.name
                        ,offices.id 
                        ,offices.type
                        ,offices.parent_office_id
                FROM ks_service.offices 
                WHERE 1=1 AND offices.id=".$office_id
             );
         $type=$data[0]['offices']['type'];
         if($type==20){
             return $data;
         }elseif($type==10){
             $data= $this->query(
                "SELECT offices.name
                        ,offices.id 
                        ,offices.type
                        ,offices.parent_office_id
                FROM ks_service.offices 
                WHERE offices.type=20 AND offices.parent_office_id=".$data[0]['offices']['id']
             );
             return $data;
         }elseif($type==30){
             $data= $this->query(
                "SELECT offices.name
                        ,offices.id 
                        ,offices.type
                        ,offices.parent_office_id
                FROM ks_service.offices 
                WHERE offices.type=20 AND offices.id=".$data[0]['offices']['parent_office_id']
             );
             return $data;
         }else{
             return NULL;
         }
    }
    public function getOffice($id){
        try{
        $data= $this->query(
                "SELECT offices.name,offices.id 
                FROM ks_service.offices 
                WHERE parent_office_id=$id 
                "
             );
        } catch (Exception $e) {
            $this->_exception(__FUNCTION__, $e, CConst::API_ERROR);
        }
        return $data;
    }

    /**
     * @author nhiendt6433 <nhiendt6433@setacinq.com.vn>
     * @since 1.0.0
     * @param array $data array
     * @return parent office from office_parent_id
     * @example  findOffice(array('office_id' => $officeId) 'first'));
     */
    public function getParentOffice($id)
    {
        # code...
    }
}
